package com.tanhua.exceptionhander;


import com.tanhua.config.ErrorResult;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

//全局异常处理
@RestControllerAdvice
public class ExceptionHander {
    //义务异常
    @ExceptionHandler(CustomException.class)
    public ResponseEntity customException(CustomException exception){
        return ResponseEntity.status(500).body(exception.getMessage());
    }

    //处理不可预知的异常
    @ExceptionHandler(Exception.class)
    public ResponseEntity handlerException1(Exception be) {
        be.printStackTrace();
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ErrorResult.error());
    }
}
